package com.yanqu.road.logic.config.unioncampwar;

import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class UnionCampWarBattleReward {
    private int startHoldId;
    private List<UnionCampWarDefeatNumConfig> numRewardList = new ArrayList<>();

    public UnionCampWarBattleReward(int startHoldId) {
        this.startHoldId = startHoldId;
    }

    /**
     * 解析次数配置
     *
     * @param value 1-101000=20=0;101000=20=1#51-101000=10;101000=10#101-101000=5;101000=5
     */
    public void initNumRewards(String value){
        String[] strAttrs = value.split("#");
        for (String strAttr : strAttrs) {
            String[] str2Attrs = strAttr.split("-");
            int winNum = Integer.valueOf(str2Attrs[0]);
            String rewards = str2Attrs[1];
            String[] rewardStrs = rewards.split(";");
            Property necessReward = new Property();
            List<PropertyWeightItem> randomRewards = new ArrayList<>();
            for (String rewardStr : rewardStrs) {
                String[] split = rewardStr.split("=");
                if (split.length < 3 || split[2].equals("0")) {
                    necessReward.addProperty(Integer.valueOf(split[0]), new BigInteger(split[1]));
                } else {
                    PropertyWeightItem weightItem = new PropertyWeightItem();
                    weightItem.setProperty(new Property(Integer.valueOf(split[0]), new BigInteger(split[1])));
                    weightItem.setWeight(Integer.valueOf(split[2]));
                    randomRewards.add(weightItem);
                }
            }

            UnionCampWarDefeatNumConfig numConfig = new UnionCampWarDefeatNumConfig();
            numConfig.setDefeatNum(winNum);
            numConfig.setRewards(necessReward);
            numConfig.setRandomRewards(randomRewards);

            this.numRewardList.add(numConfig);
        }
    }

    public int getStartHoldId() {
        return startHoldId;
    }

    public String getBattleRewards(int winNum) {
        UnionCampWarDefeatNumConfig rewardConfig = null;
        for (UnionCampWarDefeatNumConfig numReward : this.numRewardList) {
            if (winNum < numReward.getDefeatNum()) {
                break;
            }
            rewardConfig = numReward;
        }
        if (null == rewardConfig) {
            return "";
        }
        Property baseReward = new Property(rewardConfig.getRewards());
        Property randomReward = RandomWeightConfigHelper.getPropertyWeightItem(rewardConfig.getRandomRewards());
        baseReward.addProperty(randomReward);
        return PropertyHelper.parsePropertyToString(baseReward);
    }
}
